﻿using System;
using System.Collections.ObjectModel;
using System.Reactive;
using System.Windows.Media;
using OxyPlot;
using OxyPlot.Series;
using SensorDrone.Net;

namespace SensorDroneStudio.Models
{
    internal class ColrorSensorPlotModel : ISensorPlotModel
    {
        private readonly PlotModel _model;
        private readonly DateTimeOffset _now = new DateTimeOffset(DateTime.UtcNow);

        public ColrorSensorPlotModel()
        {
            _model = new PlotModel();
            _model = new PlotModel("") {Background = OxyColors.Black, TitleColor = OxyColors.White};
            var series = new PieSeries();
            series.Slices.Add(new PieSlice {Fill = OxyColors.Red, Value = 255, Label = "R"});
            series.Slices.Add(new PieSlice {Fill = OxyColors.Green, Value = 255, Label = "G"});
            series.Slices.Add(new PieSlice {Fill = OxyColors.Blue, Value = 255, Label = "B"});

            series.InnerDiameter = 0;
            series.ExplodedDistance = 0.0;
            series.Stroke = OxyColors.White;
            series.StrokeThickness = 5.0;
            series.InsideLabelPosition = 0.8;
            series.AngleSpan = 360;
            series.StartAngle = 0;
            Model.Series.Add(series);
            var summary = new PieSeries();
            summary.Slices.Add(new PieSlice("All", 100));
        }

        public ObservableCollection<Tuple<TimeSpan, byte, byte, byte>> Samples { get; set; }

        public PlotModel Model
        {
            get { return _model; }
        }

        void ISensorPlotModel.Add(Timestamped<Reading> reading)
        {
            var color = (Color) ColorConverter.ConvertFromString(reading.Value.Value);
            var series = (Model.Series[0] as PieSeries);
            series.Stroke = OxyColor.FromRgb(color.R, color.G, color.B);
            series.Slices[0].Value = color.R;
            series.Slices[1].Value = color.G;
            series.Slices[2].Value = color.B;
            Model.RefreshPlot(true);
        }

        public void Clear()
        {
        }
    }
}